package cn.wang.user.controller;

import cn.wang.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import user.pojo.User;

import javax.validation.Valid;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 校验参数的唯一性
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/check/{data}/{type}")
    public ResponseEntity<Boolean> checkUnique(
            @PathVariable("data") String data,
            @PathVariable("type") Integer type
    ){
        return ResponseEntity.ok(userService.checkUnique(data,type));
    }

    /**
     * 生成验证码
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> createCode(@RequestParam("phone") String phone){
        userService.createCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 注册接口
     * @param user
     * @param code
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code){
        userService.register(user,code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 登录接口
     */
    @GetMapping("/query")
    public ResponseEntity<User> queryUser(@RequestParam(value = "username") String username,
                                          @RequestParam(value = "password") String password){
        return ResponseEntity.ok(userService.queryUser(username,password));
    }
}
